<template>
  <div class="home-container">
    <!-- 导航栏 -->
    <van-nav-bar class="page-nav-bar" fixed>
      <van-button
        class="search-btn"
        slot="title"
        type="info"
        size="small"
        round
        icon="search"
        to="/search"
        @click="show = true"
      >搜索</van-button>
    </van-nav-bar>
    <van-popup
      closeable
      v-model="show"
      :overlay="false"
      position="top"
      :style="{ height: '30%' }"
    >ddd</van-popup>

    <!-- 滑动导航 -->
    <van-tabs v-model="active" animated swipeable @change="onChange(active)">
      <van-tab v-for="item in channels" :key="item.id" :title="item.name"></van-tab>
      <div slot="nav-right" class="placeholder"></div>
      <div slot="nav-right" class="hamburger-btn">
        <i class="toutiao">
          <van-icon name="wap-nav" @click="isShow = true"/>
        </i>
      </div>
    </van-tabs>

    <van-pull-refresh v-model="refreshing" @refresh="onRefresh">
      <van-list
        v-model="loading"
        :finished="finished"
        finished-text="没有更多了"
        @load="onLoad"
        :error.sync="error"
        error-text="请求失败，点击重新加载"
      >
        <van-cell
          v-for="article in articles"
          :key="article.art_id"
          :to="`/article/${article.art_id}`"
        >
          <div class="title van-multi-ellipsis--l2">{{ article.title }}</div>
          <div class="img-list">
            <div class="img-wrapper" v-for="(imgSrc, i) in article.cover.images" :key="i">
              <img :src="imgSrc">
            </div>
          </div>
          <div class="info">
            <div class="top is-top" v-if="true">置顶</div>
            <div class="author">{{article.aut_name}}</div>
            <div class="comment-count">评论数: {{article.comm_count}}</div>
            <div class="time">{{timestamp2fromNow(article.pubdate)}}</div>
          </div>
        </van-cell>
      </van-list>
    </van-pull-refresh>

    <!-- 频道编辑弹出层 -->
    <van-popup v-model="isShow" closeable position="bottom" :style="{ height: '100%' }">
      <ChannelEdit
        @update-active="onUpdateActive"
        :my-channels="channels"
        :allChannels="allchannels"
        :active="active"
      ></ChannelEdit>
    </van-popup>
  </div>
</template>

<script>
import { mapState, mapActions } from "vuex";
import moment from "moment";
import ChannelEdit from "./components/channel-edit";
import { withLoading } from "@/utils/request";
import { debounce } from "@/utils/index.js";
export default {
  data() {
    return {
      active: 0,
      list: [],
      loading: false,
      finished: false,
      refreshing: false,
      isShow: false,
      error: false,
      show: false
    };
  },
  computed: {
    ...mapState("articles", ["channels", "articles", "allchannels"])
  },
  methods: {
    ...mapActions("articles", [
      "updateChannels",
      "updateArticles",
      "updateAllChannels",
      "search"
    ]),
    onLoad() {
      setTimeout(() => {
        if (this.refreshing) {
          this.refreshing = false;
        }
        this.loading = false;
        this.finished = true;
      }, 1000);
    },
    async onRefresh() {
      // 清空列表数据
      this.finished = false;
      // 重新加载数据
      await this.onChange(this.active);
      // 将 loading 设置为 true，表示处于加载状态
      this.loading = true;
      this.onLoad();
    },
    timestamp2fromNow(timestamp) {
      return moment(timestamp).fromNow();
    },
    // onChange: withLoading(async function(active) {
    //   await this.updateArticles({
    //     channel_id: active,
    //     timestamp: Date.now()
    //   }).catch(() => {
    //     this.error = true
    //   });
    // }),
    // async onChange(active) {
    //   this.loading = true
    //   await this.updateArticles({
    //     channel_id: active,
    //     timestamp: Date.now()
    //   }).catch(() => {
    //     this.error = true
    //   });
    //   this.loading = false
    // },
    onDebounceChange: debounce(function(active) {
      this.onChange(active);
    }, 500),
    onChange: debounce(async function(active) {
      this.loading = true;
      await this.updateArticles({
        channel_id: active,
        timestamp: Date.now()
      }).catch(() => {
        this.error = true;
      });
      this.loading = false;
    }),
    onUpdateActive(index, isShow = true) {
      console.log("home", index);
      this.active = index;
      this.updateArticles({
        channel_id: index,
        timestamp: Date.now()
      });
      this.isShow = isShow;
    }
  },
  async mounted() {
    await this.updateChannels();
    this.updateAllChannels();
    // withLoading(async function() {
    //   await this.updateArticles({
    //     channel_id: this.channels[0].id,
    //     timestamp: Date.now()
    //   }).catch(() => {
    //     this.error = true
    //   });
    // });
    this.updateArticles({
      channel_id: this.channels[0].id,
      timestamp: Date.now()
    }).catch(() => {
      this.error = true;
    });
    this.loading = false;
  },
  components: {
    ChannelEdit
  }
};
</script>

<style lang="less" scoped>
.home-container {
  ::v-deep .van-nav-bar__title {
    max-width: unset;
  }
  .search-btn {
    width: 555px;
    height: 64px;
    background-color: #5babfb;
    border: none;
    font-size: 35px;
    .van-icon {
      font-size: 32px;
    }
  }
}
.van-icon-search:before {
  font-size: 45px;
  color: #fff;
}
::v-deep .channel-tabs {
  .van-tab {
    border-right: 1px solid #edeff3;
    min-width: 200px;
    height: 80px;
    .van-tab__text {
      font-size: 27px;
      color: #777777;
    }
  }
  .van-tab--active .van-tab__text {
    color: #333 !important;
  }
  .van-tabs__line {
    width: 31px !important;
    height: 6px;
    background: rgba(50, 150, 250, 1);
    border-radius: 3px;
    bottom: 8px;
  }
  .van-tabs__nav {
    padding: 0;
  }
}
.placeholder {
  flex-shrink: 0;
  width: 66px;
  height: 82px;
}
.van-icon-bar-chart-o:before {
  font-size: 55px;
}
.hamburger-btn {
  position: fixed;
  right: 0;
  display: flex;
  justify-content: center;
  align-items: center;
  width: 66px;
  height: 82px;
  background-color: #fff;
  opacity: 0.902;
  i.toutiao {
    margin-top: 10px;
    font-size: 40px;
  }
  &:before {
    content: "";
    position: absolute;
    left: 0;
    width: 1px;
    height: 100%;
    background-image: url(~@/assets/gradient-gray-line.png);
    background-size: contain;
  }
}
.van-tabs {
  top: 90px;
  left: 0;
  z-index: 1;
}
.van-list {
  margin-top: 100px;
  height: 79vh;
  overflow: auto;
}

.title {
  word-break: break-all;
  font-size: 30px;
}

.img-list {
  width: 100%;
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;

  .img-wrapper {
    height: 150px;
    overflow: hidden;
    position: relative;

    img {
      display: block;
      width: 100%;
      height: 100%;
      // center;
    }
  }
}
.info {
  width: 100%;
  overflow: hidden;

  .is-top {
    color: red;
  }

  & > div {
    float: left;
    font-size: 10px;
    margin-right: 12px;
    color: #d6d6d6;
  }
}
.van-popup {
  margin-top: 100px;
}
</style>